var app = angular.module('crawlerApp');

app.controller('taskInfoCtrl', ['$scope', '$interval', 'ngDialog', 'toastr',
  'taskHelper',
  function($scope, $interval,
    ngDialog, toastr, taskHelper) {

    $scope.prepare = {
      result: []
    };
    $scope.running = {
      result: []
    };
    $scope.complete = {
      result: []
    };

    $scope.getStatus = function() {
      taskHelper.getStatus().then(function(res) {
        if (res.data.success) {
          $scope.prepare.result = res.data.list.prepareList;
          $scope.running.result = res.data.list.runningList;
          $scope.complete.result = res.data.list.completeList;
        }
      });
    };
    // var cnt = 0;
    var timer = $interval($scope.getStatus, 1000);

    $scope.$on('$destroy', function() {
      $interval.cancel(timer);
      console.log('cancel timer');
    });

    $scope.fields = {
      all: [{
        name: "任务ID",
        value: "id"
      }, {
        name: "项目名称",
        value: "name"
      }, {
        name: "所属分组",
        value: "groupID"
      }, {
        name: "优先级",
        value: "previous"
      }, {
        name: "采集结果",
        value: "items"
      }, {
        name: "发出请求",
        value: "request"
      }, {
        name: "错误",
        value: "error"
      }, {
        name: "日志",
        value: "log"
      }, {
        name: "等待时间",
        value: "waiting"
      }, {
        name: "运行时间",
        value: "time"
      }, {
        name: "状态",
        value: "status"
      }],

      selected: [{
        name: "任务ID",
        value: "id"
      }, {
        name: "项目名称",
        value: "name"
      }, {
        name: "所属分组",
        value: "groupID"
      }, {
        name: "优先级",
        value: "previous"
      }, {
        name: "状态",
        value: "status"
      }]
    };

    $scope.build = function() {
      var dialog = ngDialog.openConfirm({
        template: 'src/task/tmpl/ng-dialog-run.html',
        controller: ['$scope', '$filter', 'taskHelper', startTaskCtrl]
      });
      dialog.then(function(res) {
        console.log(res, 'run');
        toastr.info('', res.data.msg.msg);
        // 启动成功
      }, function(rea) {
        // 取消启动
      })
    };

    $scope.stop = function() {

    };

    // 全选
    $scope.selectall = function(list) {
      // console.log(group.checked);
      // group.checked = group.checked == undefined ? true : !group.checked;
      var tasks = list.result;
      for (var i = 0; i < tasks.length; i++) {
        tasks[i].checked = list.checked;
      }
    };

    // function checkedList() {
    //   var res = [];
    //   $scope.tasks.filter(function(status) {
    //     var list = status.taskInfo.filter(function(task) {
    //       return task.checked == true;
    //     });
    //     res.push(list);
    //   })
    //   return res;
    // };

    // $scope.selectall = function() {
    //
    // };

    var startTaskCtrl = function(scope, filter, taskHelper) {
      scope.task = {
        group: [],
        previous: '1'
      };
      scope.groups = [{
        'groupID': 'group1',
        'projects': [{
          "previous": "1",
          "name": "project1",
          "groupID": "group1",
          "depth": -1,
          "start_url": "https://segmentfault.com/a/1190000002691861",
          "rule_store": [{
            "url_pattern": [
              "https://segmentfault.com.*?"
            ],
            "link_parse_rule": [{
              "mode": "reg",
              "expression": "href=\"(https://github.com/totorojs/totoro)\""
            }],
            "content_parse_rule": {},
            "request_options": {},
            "saveAddress": "/Users/lawwy/Work/project/wf-spider/lib/downloader/html/"
          }]
        }, {
          "previous": "1",
          "name": "project2",
          "groupID": "group1",
          "depth": -1,
          "start_url": "https://segmentfault.com/a/1190000002691861",
          "rule_store": [{
            "url_pattern": [
              "https://segmentfault.com.*?"
            ],
            "link_parse_rule": [{
              "mode": "reg",
              "expression": "href=\"(https://github.com/totorojs/totoro)\""
            }],
            "content_parse_rule": {},
            "request_options": {},
            "saveAddress": "/Users/lawwy/Work/project/wf-spider/lib/downloader/html/"
          }]
        }, {
          "previous": "1",
          "name": "project3",
          "groupID": "group1",
          "depth": -1,
          "start_url": "https://segmentfault.com/a/1190000002691861",
          "rule_store": [{
            "url_pattern": [
              "https://segmentfault.com.*?"
            ],
            "link_parse_rule": [{
              "mode": "reg",
              "expression": "href=\"(https://github.com/totorojs/totoro)\""
            }],
            "content_parse_rule": {},
            "request_options": {},
            "saveAddress": "/Users/lawwy/Work/project/wf-spider/lib/downloader/html/"
          }]
        }]
      }, {
        'groupID': 'group2',
        'projects': [{
          "previous": "1",
          "name": "project1",
          "groupID": "group2",
          "depth": -1,
          "start_url": "https://segmentfault.com/a/1190000002691861",
          "rule_store": [{
            "url_pattern": [
              "https://segmentfault.com.*?"
            ],
            "link_parse_rule": [{
              "mode": "reg",
              "expression": "href=\"(https://github.com/totorojs/totoro)\""
            }],
            "content_parse_rule": {},
            "request_options": {},
            "saveAddress": "/Users/lawwy/Work/project/wf-spider/lib/downloader/html/"
          }]
        }, {
          "previous": "1",
          "name": "project2",
          "groupID": "group2",
          "depth": -1,
          "start_url": "https://segmentfault.com/a/1190000002691861",
          "rule_store": [{
            "url_pattern": [
              "https://segmentfault.com.*?"
            ],
            "link_parse_rule": [{
              "mode": "reg",
              "expression": "href=\"(https://github.com/totorojs/totoro)\""
            }],
            "content_parse_rule": {},
            "request_options": {},
            "saveAddress": "/Users/lawwy/Work/project/wf-spider/lib/downloader/html/"
          }]
        }, {
          "previous": "1",
          "name": "project3",
          "groupID": "group2",
          "depth": -1,
          "start_url": "https://segmentfault.com/a/1190000002691861",
          "rule_store": [{
            "url_pattern": [
              "https://segmentfault.com.*?"
            ],
            "link_parse_rule": [{
              "mode": "reg",
              "expression": "href=\"(https://github.com/totorojs/totoro)\""
            }],
            "content_parse_rule": {},
            "request_options": {},
            "saveAddress": "/Users/lawwy/Work/project/wf-spider/lib/downloader/html/"
          }]
        }]
      }];
      scope.priorities = [{
        'name': '最高',
        'value': '0'
      }, {
        'name': '正常',
        'value': '1'
      }, {
        'name': '最低',
        'value': '2'
      }];
      scope.start = function(task) {
        scope.errMessage = '';
        taskHelper.buildTask(filter('taskConfigFormat')(task)).then(
          function(res) {
            // console.log(res, 'success');
            // msg:true false?
            scope.confirm(res);

          },
          function(rea) {
            // todo
            scope.errMessage = rea + ',请重试';
            console.log(rea, '失败');
          });
      };
    }

  }
]);
